﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using University.Workshop.DataAccess;

namespace University.Workshop.Business
{
    public static class PrivacySettingsManager
    {
        public static PrivacySetting SetPrivacySettingByEmail(string email, string setting)
        {
            User relatedUser = UserDataAccess.GetUserByEmail(email);
            if (relatedUser != null)
            {
                PrivacySetting userPrivacySetting = PrivacySettingDataAccess.GetPrivacySettingByUserId(relatedUser.Id);
                if (userPrivacySetting == null)
                {
                    userPrivacySetting = new PrivacySetting { UserId = relatedUser.Id, Setting = setting };
                    return PrivacySettingDataAccess.SetPrivacySetting(userPrivacySetting);
                }
                else
                {
                    return PrivacySettingDataAccess.EditPrivacySetting(setting, relatedUser.Id);
                }
            }
            return null;
        }

        public static PrivacySetting GetPrivacySettingByEmail(string email)
        {
            User relatedUser = UserDataAccess.GetUserByEmail(email);
            if (relatedUser != null)
            {
                PrivacySetting userPrivacySetting = PrivacySettingDataAccess.GetPrivacySettingByUserId(relatedUser.Id);
                if (userPrivacySetting == null)
                {
                    return new PrivacySetting { UserId = relatedUser.Id, Setting = "000000000" };
                }
                return userPrivacySetting;
            }
            return null;
        }

        public static Collection<Product> GetShownProducts(long userId)
        {
            Collection<Product> allProducts = ProductDataAccess.GetProductsBySeller(userId, true);
            if (allProducts != null)
            {
                for (int i = 0; i < allProducts.Count; i++)
                {
                    if (!allProducts[i].IsShown)
                    {
                        allProducts.RemoveAt(i);
                        i--;
                    }
                }
                return allProducts;
            }
            return null;
        }

        public static void SetShownProducts(Collection<Product> shownProducts, Collection<Product> allProducts, bool updateShown )
        {
            if (allProducts != null)
            {
                foreach (var product in allProducts)
                {
                    product.IsShown = false;
                    ProductDataAccess.ModifyProduct(product);
                }
                if (updateShown && shownProducts!=null)
                {
                    foreach (var prod in shownProducts)
                    {
                        prod.IsShown = true;
                        ProductDataAccess.ModifyProduct(prod);
                    }
                }
            }
        }
    }
}
